package com.leetcode.根据算法进行分类.排序算法相关.冒泡排序;

import java.util.Arrays;

/**
 * @author: ZhouBert
 * @date: 2021/3/9
 * @description: 283. 移动零
 * https://leetcode-cn.com/problems/move-zeroes/
 */
public class A_283_移动零 {

	static A_283_移动零 action = new A_283_移动零();

	public static void main(String[] args) {
		test1();
	}

	public static void test1() {
		//
		int[] nums = new int[]{
				0, 1, 0, 3, 12
		};
		action.moveZeroes(nums);
		System.out.println(Arrays.toString(nums));
	}

	/**
	 * 将非 0 的搬到前面，后面的补零
	 * @param nums
	 */
	public void moveZeroes(int[] nums) {
		int count = 0;
		int len = nums.length;
		for (int i = 0; i < len; i++) {
			if (nums[i] == 0) {
				count++;
			} else {
				nums[i - count] = nums[i];
			}
		}

		//剩余的用 0 补全
		for (int j = len - count; j < len; j++) {
			nums[j] = 0;
		}
	}
}
